<?php
class model_users extends database_mysql
{
	public function mdu_login($email,$pass)
    {
        $email = parent::mysql_check_string($email);
        $pass = md5(md5($pass));
        $pass =   parent::mysql_check_string($pass);  
        
        $row = parent::mysql_fetch_one_rows("
            SELECT
                ". prefix ."users.ID,
                ". prefix ."users.Email,
                ". prefix ."users.Fullname,
                ". prefix ."users.Sex,
                ". prefix ."users.`group`
            FROM
                ". prefix ."users
            WHERE
                ". prefix ."users.Email = '".$email."' AND
                ". prefix ."users.`Password` = '".$pass."' AND
                ". prefix ."users.`Status` = 'true' 
        ");
        
 		return $row;
    }
    
    public function mdu_tool_func($table, $model,$active,$str_id)
    {
        $done = false;
        switch($active)
        {
            case 'show':
            case 'hide':
                $status = array('show'=>'true', 'hide'=>'false');
                $done = $model->model_tool_set_status(prefix.$table,$status[$active]," ID IN(".trim($str_id).")");
                
            break;

            case 'delete':
                $done = $model->model_tool_delete(prefix.$table," ID IN(".trim($str_id).")" );
                $done = $model->model_tool_delete(prefix.'user_group'," UserID IN(".trim($str_id).")" );
            break;
        }

        return $done;
    }
    
    /*************************phung*****************************/
    public function mdu_get_list_user($limit = 10, $start = 1)
    {
        if($start == 1)
        {
            $start = 0;
        }else{
            $start = ($start*$limit)-$limit;
        }
        $sql = " SELECT * FROM ". prefix ."users ORDER BY `". prefix ."users`.`ID` DESC LIMIT $start , $limit";
        $row = parent::mysql_fetch_all_rows($sql);
 		return $row;
    }
    
    public function mdu_insert_user($table, $data)
    {
        $table = prefix ."users";
        $this->mysql_insert_array($table, $data);
    }
    
    public function mdu_insert_group_user($user_id, $group)
    {
        foreach($group as $value){
            $sql = "INSERT INTO `". prefix ."user_group` (`UserID` ,`GroupID`) VALUES ('".$user_id."', '".$value."')";
            $this->mysql_update_insert($sql);
        }
        
    }
    
    public function mdu_get_last_id()
    {
        $result = parent::mysql_selected("SELECT ID FROM ". prefix ."users ORDER BY id DESC LIMIT 1");
        $result = mysql_fetch_array($result);
        return $result['ID'];

    }
    
    public function mdu_get_group(){
        $sql = "
                SELECT
                ". prefix ."groups.GroupName,
                ". prefix ."groups.ID
                FROM
                ". prefix ."groups
                WHERE
                ". prefix ."groups.`Status` = 'true'
        ";
        $row = parent::mysql_fetch_all_rows($sql);
        return $row;
    }
    
    public function mdu_get_user_from_id($id){
        $row = parent::mysql_fetch_one_rows(" 
                    SELECT *
                    FROM
                    ". prefix ."users
                    WHERE
                    ". prefix ."users.ID = 
        ".$id);
 		return $row;
    }

    public function mdu_get_user_from_email($email){
        $email = parent::mysql_check_string($email);
        $row = parent::mysql_fetch_one_rows("
                    SELECT `ID`,`Email`,`Fullname`,`Sex`
                    FROM
                    ". prefix ."users
                    WHERE
                    ". prefix ."users.Email =
        '$email'");
 		return $row;
    }
    public function mdu_update_pass($pass , $id){
        $pass = parent::mysql_check_string($pass);
        $tb_user = prefix . "users";
        $row = parent::mysql_update_insert("UPDATE `$tb_user` SET `Password`='$pass' WHERE `ID` = '$id'");
 		return $row;
    }

    public function mdu_list_user_group($id)
    {
        $row = parent::mysql_fetch_all_rows("
                SELECT *
                FROM
                ". prefix ."groups
                INNER JOIN ". prefix ."user_group ON ". prefix ."user_group.GroupID = ". prefix ."groups.ID
                WHERE
                ". prefix ."groups.`Status` = 'true' AND
                ". prefix ."user_group.UserID = 
        
        " . $id);
 		return $row;
    }
    
    public function mdu_del_group_user($id)
    {
        parent::mysql_delete("DELETE FROM `". prefix ."user_group` WHERE `". prefix ."user_group`.`UserID` IN($id)");
        
    }
    
    public function mdu_model_update_user($data, $condition)
    {
        $table = prefix ."users";
        parent::mysql_update_array($table, $data, $condition);
    }
    
    public function mdu_get_total($table)
    {
        $sql = "SELECT COUNT( * ) AS total FROM ". prefix .$table;
        $result = parent::mysql_select_one($sql);
        return  $result;
    }
}

